#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(int i = (a);i <= (b);++i)
#define per(i,a,b) for(int i = (a);i >= (b);--i) 
#define mkp std::make_pair
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;
inline bool cmp(int x,int y){return x < y;}
/* -fsanitize=undefined  */
const int N = 2e5+9;
const int inf = 1e9+9;
const double eps = 1e-7;
int _,n,m,s,a[2*N],u,v,w;
ll dis[2*N];
bool vis[2*N];

int head[2*N],next[2*N],ver[2*N],tot;
ll edge[2*N];
inline void link(int x,int y){ver[++tot] = y , edge[tot] = w , next[tot] = head[x] , head[x] = tot;}

std::priority_queue< std::pair<ll,int> > q;

inline void dij(){
    while(q.size()) q.pop();
    q.push( mkp(0,s) );
    while(q.size()){
        int now = q.top().second;   q.pop();
        if(vis[now]) continue;
        vis[now] = true;
        for(int i = head[now];i;i = next[i]){
            if( dis[ ver[i] ] > dis[ now ] + edge[i]){
                dis[ ver[i] ] = dis[ now ] + edge[i];
                q.push( mkp(-dis[ ver[i] ],ver[i]) );
            }
        }
    }
}

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef LOCAL //  -DLOCAL
        freopen("in.in", "r", stdin);
    #endif
    cin >> n >> m >> s;
    rep(i,1,m){
        cin >> u >> v >> w;
        link(u,v);
    }
    std::memset(dis,inf,sizeof(dis));
    dis[s] = 0;
    dij();
    rep(i,1,n) cout << dis[i] << " ";

    return 0;
}